package com.hw.daobean;

import com.hw.HWApp;
import com.hw.dao.ProductInfoDao;
import com.hw.dao.RelationInfoDao;
import com.hw.dao.RelationUpInfoDao;

import java.util.ArrayList;
import java.util.List;

public class DataBaseControl {
    public static String tag = "DataBaseControl";

    //////////////////////////产品
    //增加产品信息
    public static long insertProduct(ProductInfo entity) {
        long res = -1;
        try {
            res = HWApp.daoSession.getProductInfoDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除产品信息
    public static void deleteProduct() {
        try {
            HWApp.daoSession.getProductInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询全部产品信息
    public static List<ProductInfo> searchP() {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        try {
            list = HWApp.daoSession.getProductInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //查询产品信息带条件
    public static List<ProductInfo> searchPid(String id) {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        try {
            list = HWApp.daoSession.getProductInfoDao().queryBuilder().where(ProductInfoDao.Properties.Iid.in(id)).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //查询产品信息带条件 唯一
    public static ProductInfo searchUnPid(String id) {
        ProductInfo productInfo = new ProductInfo();
        try {
            productInfo = HWApp.daoSession.getProductInfoDao().queryBuilder().where(ProductInfoDao.Properties.Iid.eq(id)).unique();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return productInfo;
        }
    }
    //////////////////////////产品


    ///////////////////已上传数码关系
    //查询数码关系
    public static List<RelationInfo> searchRelation() {
        List<RelationInfo> list = new ArrayList<RelationInfo>();
        try {
            list = HWApp.daoSession.getRelationInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //增加数码关系
    public static long addRelation(RelationInfo relationInfo) {
        long res = -1;
        try {
            res = HWApp.daoSession.getRelationInfoDao().insert(relationInfo);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除全部数码关系
    public static void deleteRelationAlls() {
        try {
            HWApp.daoSession.getRelationInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除全部数码关系
    public static void deleteRelationAll(List<RelationInfo> list) {
        try {
            HWApp.daoSession.getRelationInfoDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除数码关系
    public static void deleteRelation(String pid, String batch, String casecode) {
        try {
            List<RelationInfo> list = HWApp.daoSession.getRelationInfoDao().queryBuilder().where
                    (RelationInfoDao.Properties.Pid.eq(pid)
                            , RelationInfoDao.Properties.Batch.eq(batch)
                            , RelationInfoDao.Properties.Casecode.eq(casecode)).list();
            HWApp.daoSession.getRelationInfoDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询是否存在箱码
    public static boolean containCase(String casecode) {
        boolean flag = false;
        try {
            List<RelationInfo> list = new ArrayList<RelationInfo>();
            list = HWApp.daoSession.getRelationInfoDao().queryBuilder().where(RelationInfoDao.Properties.Casecode.in(casecode)).list();
            if (list.size() == 0) {
                flag = false;
            } else {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = true;
        } finally {
            return flag;
        }
    }

    //查询是否存在盒码
    public static boolean containBox(String boxcode) {
        boolean flag = false;
        try {
            RelationInfo relationInfo = null;
            relationInfo = HWApp.daoSession.getRelationInfoDao().queryBuilder().where(RelationInfoDao.Properties.Boxcode.in(boxcode)).unique();
            if (relationInfo == null) {
                flag = false;
            } else {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = true;
        } finally {
            return flag;
        }
    }

    //查询指定产品 批号生产的
    public static List<RelationInfo> getlist(String pid, String batch) {
        List<RelationInfo> list = new ArrayList<RelationInfo>();
        try {
            list = HWApp.daoSession.getRelationInfoDao().queryBuilder().where(RelationInfoDao.Properties.Pid.in(pid)
                    , RelationInfoDao.Properties.Batch.in(batch)
            ).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }
    ///////////////////已上传数码关系


    ///////////////////待上传数码关系
    //删除全部待上传数码关系
    public static void deleteUpRelationAlls() {
        try {
            HWApp.daoSession.getRelationUpInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询数码关系
    public static List<RelationUpInfo> searchUpRelation() {
        List<RelationUpInfo> list = new ArrayList<RelationUpInfo>();
        try {
            list = HWApp.daoSession.getRelationUpInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //增加数码关系
    public static long addUpRelation(RelationUpInfo relationUpInfo) {
        long res = -1;
        try {
            res = HWApp.daoSession.getRelationUpInfoDao().insert(relationUpInfo);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除数码关系
    public static void deleteUpRelation(String pid, String batch, String casecode) {
        try {
            List<RelationUpInfo> list = HWApp.daoSession.getRelationUpInfoDao().queryBuilder().where
                    (RelationUpInfoDao.Properties.Pid.eq(pid)
                            , RelationUpInfoDao.Properties.Batch.eq(batch)
                            , RelationUpInfoDao.Properties.Casecode.eq(casecode)).list();
            HWApp.daoSession.getRelationUpInfoDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询是否存在箱码
    public static boolean containUpCase(String casecode) {
        boolean flag = false;
        try {
            List<RelationUpInfo> list = new ArrayList<RelationUpInfo>();
            list = HWApp.daoSession.getRelationUpInfoDao().queryBuilder().where(RelationUpInfoDao.Properties.Casecode.in(casecode)).list();
            if (list.size() == 0) {
                flag = false;
            } else {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = true;
        } finally {
            return flag;
        }
    }

    //查询是否存在盒码
    public static boolean containUpBox(String boxcode) {
        boolean flag = false;
        try {
            RelationUpInfo relationUpInfo = null;
            relationUpInfo = HWApp.daoSession.getRelationUpInfoDao().queryBuilder().where(RelationUpInfoDao.Properties.Boxcode.in(boxcode)).unique();
            if (relationUpInfo == null) {
                flag = false;
            } else {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = true;
        } finally {
            return flag;
        }
    }

    //查询指定产品 批号生产的
    public static List<RelationUpInfo> getUplist(String pid, String batch) {
        List<RelationUpInfo> list = new ArrayList<RelationUpInfo>();
        try {
            list = HWApp.daoSession.getRelationUpInfoDao().queryBuilder().where(RelationUpInfoDao.Properties.Pid.in(pid)
                    , RelationUpInfoDao.Properties.Batch.in(batch)
            ).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //查询箱码所在盒码
    public static List<String> getUplist_case( String casecode) {
        List<String> liststr = new ArrayList<String>();
        List<RelationUpInfo> list = new ArrayList<RelationUpInfo>();
        try {
            list = HWApp.daoSession.getRelationUpInfoDao().queryBuilder().where(
                    RelationUpInfoDao.Properties.Casecode.eq(casecode)
                    ).list();
            for (int i = 0; i <list.size() ; i++) {
                liststr.add(list.get(i).getBoxcode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return liststr;
        }
    }
    ///////////////////待上传数码关系
}


